public class ChatPresenter implements TIMMessageListener，TIMMessageRevokedListener{

    private ChatView view;
    private TIMConversation conversation;
    private boolean downing = false;
    private final int Num = 20;

    //构造函数
    public ChatPresenter(ChatView view,String identify,TIMConversationType type){
        this.view = view;
        conversation = TIMManager.getInstance().getConversation(type,identify);
    }

    //响应新消息接口
    @Override
    public boolean onNewMessages(List<TIMMessage> list) {
     for (TIMMessage item:list){
            update(item);
         }
     return false;
    }
    
    //响应消息撤回接口
    @Override
    public void onMessageRevoked(TIMMessageLocator timMessageLocator) {
        update(timMessageLocator);
    }

    //载入
    public void start() {
        getMessage(null);
        TIMConversationExt timConversationExt = new TIMConversationExt(conversation);
        if (timConversationExt.hasDraft()){
            view.showDraft(timConversationExt.getDraft());
        }
    }

    //发送消息
    public void sendMessage(final TIMMessage message) {
        conversation.sendMessage(message, new TIMValueCallBack<TIMMessage>() {
            @Override
            public void onError(int code, String desc) {
                view.onSendMessageFail(code, desc, message);
            }
            @Override
            public void onSuccess(TIMMessage msg) {
                update(null);
            }
        });
        update(message);
    }

    //撤回消息
    public void revokeMessage(final TIMMessage message) {
        TIMConversationExt timConversationExt = new TIMConversationExt(conversation);
        timConversationExt.revokeMessage(message, new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                view.showToast(i+":"+s);
            }

            @Override
            public void onSuccess() {
                update(null);
            }
        });
    }

    //更新界面
    private void update(Object message) {
         if (message instanceof TIMMessage || message == null) {
             TIMMessage msg = (TIMMessage) message;
             if (msg==null||msg.getConversation().getPeer().equals(conversation.getPeer())&&msg.getConversation().getType()==conversation.getType()){
                 view.showMessage(msg);
                 readMessages();
            }
         }else if (message instanceof TIMMessageLocator) {
            TIMMessageLocator msg = (TIMMessageLocator) message;
            view.showRevokeMessage(msg);
         }
    }

    //获取消息
    public void getMessage(@Nullable TIMMessage message){
        if (!downing){
            downing = true;
            TIMConversationExt timConversationExt = new TIMConversationExt(conversation);
            timConversationExt.getMessage(Num, message, new TIMValueCallBack<List<TIMMessage>>() {
                @Override
                public void onError(int i, String s) {
                    downing = false;
                }
                @Override
                public void onSuccess(List<TIMMessage> timMessages) {
                    downing = false;
                    view.showMessage(timMessages);
                }
            });
        }
    }

    //已读全部消息
    public void readMessages(){
        TIMConversationExt timConversationExt = new TIMConversationExt(conversation);
        timConversationExt.setReadMessage(null, null);
    }

    //保留草稿
    public void saveDraft(TIMMessage message){
        TIMConversationExt timConversationExt = new TIMConversationExt(conversation);
        timConversationExt.setDraft(null);
        if (message != null && message.getElementCount() > 0){
            TIMMessageDraft draft = new TIMMessageDraft();
            for (int i = 0; i < message.getElementCount(); ++i){
                draft.addElem(message.getElement(i));
            }
            timConversationExt.setDraft(draft);
        }
    }

    //对外传递conversation
    public TIMConversation getConversation(){
        return conversation;
    }
  

}
